/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyecto;

import com.proyecto.datos.EntityAlumno;
import com.proyecto.datos.EntityCarrera;
import com.proyecto.modelado.Alumno;
import com.proyecto.modelado.Carrera;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mario
 */
public class frmModificarAlumnosIntro extends javax.swing.JInternalFrame {

    Alumno al;
    ArrayList arryAl;
    EntityCarrera entCarre;
    ArrayList arryCarre;
    Carrera carre;

    public void PopularTabla() {

        DefaultTableModel tabla;
        Object[] fila = new Object[5];

        try {

            String[] columnName = {"ID", "Carnet", "Nombre", "Apellido", "Carrera"};
            tabla = new DefaultTableModel(null, columnName);

            //Populando tabla


            arryAl = new ArrayList();
            al = new Alumno();
            EntityAlumno entAl = new EntityAlumno();
            arryAl = al.MostrarAlumnos();

            if (arryAl != null) {
                for (int i = 0; i < arryAl.size(); i++) {
                    entAl = (EntityAlumno) arryAl.get(i);
                    fila[0] = entAl.getId_alumno();
                    fila[1] = entAl.getCarnet();
                    fila[2] = entAl.getNombres();
                    fila[3] = entAl.getApellidos();
                    fila[4] = entAl.getEntCarrera();
                    tabla.addRow(fila);
                }
                this.jtbleModificarAlumnos.setModel(tabla);
            } else {
                JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la base de datos", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString());
        }

    }

    public void BloquearControles() {

        this.jtxtID.setText("ID");
        this.jtxtNombres.setText("Nombres");
        this.jtxtApellidos.setText("Apellidos");
        this.jcmbCarrera.setSelectedItem("Carrera");
        this.jtxtCarnet.setText("Carnet");

        this.jtxtID.setEnabled(false);
        this.jtxtNombres.setEnabled(false);
        this.jtxtApellidos.setEnabled(false);
        this.jcmbCarrera.setEnabled(false);
        this.jtxtCarnet.setEnabled(false);

    }

    public void HabilitarControles() {

        this.jtxtID.setEnabled(false);
        this.jtxtNombres.setEnabled(true);
        this.jtxtApellidos.setEnabled(true);
        this.jcmbCarrera.setEnabled(true);
        this.jtxtCarnet.setEnabled(true);
    }

    public void CargarComboBox() {

        // Cargamos los combobox
        entCarre = null;
        carre = new Carrera();

        try {
            // Autores
            arryCarre = new ArrayList();
            arryCarre = carre.MostrarCarrera();
            JList list_1 = new JList();
            DefaultListModel listModel_1 = new DefaultListModel();
            listModel_1.addElement("Carrera");
            if (arryCarre != null) {
                for (int i = 0; i < arryCarre.size(); i++) {
                    EntityCarrera entCarre = (EntityCarrera) arryCarre.get(i);
                    listModel_1.addElement(entCarre.getNombre_carrera());
                }
            } else {
                JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la tabla Autor", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
            }
            list_1.setModel(listModel_1);
            this.jcmbCarrera.setModel(new DefaultComboBoxModel(listModel_1.toArray()));
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    /**
     * Creates new form frmModificarAlumnosIntro
     */
    public frmModificarAlumnosIntro() {
        initComponents();

        CargarComboBox();
        BloquearControles();
        PopularTabla();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbleModificarAlumnos = new javax.swing.JTable();
        jbtnActualizar = new javax.swing.JButton();
        jtxtID = new javax.swing.JTextField();
        jtxtNombres = new javax.swing.JTextField();
        jtxtApellidos = new javax.swing.JTextField();
        jcmbCarrera = new javax.swing.JComboBox();
        jbtnCargar = new javax.swing.JButton();
        jtxtCarnet = new javax.swing.JTextField();
        jbtnEliminar = new javax.swing.JButton();

        setClosable(true);
        setResizable(true);

        jtbleModificarAlumnos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbleModificarAlumnos);

        jbtnActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/editar.png"))); // NOI18N
        jbtnActualizar.setText("Actualizar");
        jbtnActualizar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnActualizarMouseClicked(evt);
            }
        });

        jtxtID.setText("ID");

        jtxtNombres.setText("Nombres");

        jtxtApellidos.setText("Apellidos");

        jcmbCarrera.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Carrera" }));

        jbtnCargar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/cargar.png"))); // NOI18N
        jbtnCargar.setText("Cargar");
        jbtnCargar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCargarMouseClicked(evt);
            }
        });

        jtxtCarnet.setText("Carnet");

        jbtnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/images (2).jpg"))); // NOI18N
        jbtnEliminar.setText("Eliminar");
        jbtnEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnEliminarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jtxtID, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jtxtCarnet, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtxtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcmbCarrera, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(231, 231, 231)
                .addComponent(jbtnCargar)
                .addGap(29, 29, 29)
                .addComponent(jbtnEliminar)
                .addGap(33, 33, 33)
                .addComponent(jbtnActualizar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtCarnet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcmbCarrera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnActualizar)
                    .addComponent(jbtnCargar)
                    .addComponent(jbtnEliminar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnActualizarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnActualizarMouseClicked
        // TODO add your handling code here:

        try {
            int id_alumno = Integer.parseInt(jtxtID.getText());
            String carnet = this.jtxtCarnet.getText();
            String nombre = this.jtxtNombres.getText();
            String apellido = this.jtxtApellidos.getText();
            
            int id_carrera = 0;
            for (int i = 0; i < arryCarre.size(); i++) {
                entCarre = (EntityCarrera) arryCarre.get(i);
                if (this.jcmbCarrera.getSelectedItem().equals(entCarre.getNombre_carrera())) {
                    id_carrera = entCarre.getId_carrera();
                }
            }
            
            
            String msj = al.ModificarAlumno(id_alumno,nombre,apellido,carnet,id_carrera);
            JOptionPane.showMessageDialog(rootPane, msj, "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
            // Inicializar los controles
            BloquearControles();
            // Cargamos nuevamente los datos de la tabla
            PopularTabla();
            
            
        }catch(Exception ex){
             JOptionPane.showMessageDialog(rootPane, "No se encontró ningún dato a modificar", "CONFIRMACIÓN", JOptionPane.ERROR_MESSAGE);
        }
   
        
    }//GEN-LAST:event_jbtnActualizarMouseClicked

    private void jbtnCargarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCargarMouseClicked
        // TODO add your handling code here:

        HabilitarControles();
        CargarComboBox();


        int fila = this.jtbleModificarAlumnos.getSelectedRow();

        this.jtxtID.setText(String.valueOf(this.jtbleModificarAlumnos.getValueAt(fila, 0)));
        this.jtxtCarnet.setText(String.valueOf(this.jtbleModificarAlumnos.getValueAt(fila, 1)));
        this.jtxtNombres.setText(String.valueOf(this.jtbleModificarAlumnos.getValueAt(fila, 2)));
        this.jtxtApellidos.setText(String.valueOf(this.jtbleModificarAlumnos.getValueAt(fila, 3)));
        this.jcmbCarrera.setSelectedItem(String.valueOf(this.jtbleModificarAlumnos.getValueAt(fila, 4)));
    }//GEN-LAST:event_jbtnCargarMouseClicked

    private void jbtnEliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnEliminarMouseClicked
        // TODO add your handling code here:
        try {
            int id_alumno = Integer.parseInt(this.jtxtID.getText());
            String msj = al.EliminarAlumno(id_alumno);
            JOptionPane.showMessageDialog(rootPane, msj, "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
            // Inicializar los controles
            BloquearControles();
            // Cargamos nuevamente los datos de la tabla
            PopularTabla();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, "No se encontró ningún dato a eliminar", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jbtnEliminarMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnActualizar;
    private javax.swing.JButton jbtnCargar;
    private javax.swing.JButton jbtnEliminar;
    private javax.swing.JComboBox jcmbCarrera;
    private javax.swing.JTable jtbleModificarAlumnos;
    private javax.swing.JTextField jtxtApellidos;
    private javax.swing.JTextField jtxtCarnet;
    private javax.swing.JTextField jtxtID;
    private javax.swing.JTextField jtxtNombres;
    // End of variables declaration//GEN-END:variables
}
